package priv.wangy.micro.common.po.valid.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import priv.wangy.micro.common.po.valid.StringValues;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author zhangjq
 * @since 2023-12-26
 */
public class StringValuesValidator implements ConstraintValidator<StringValues, String> {

    private String[] values;

    @Override
    public void initialize(StringValues constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        values = constraintAnnotation.values();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        return Objects.nonNull(value) && Arrays.asList(values).contains(value);
    }
}
